package com.api.weavernorth.action.sick;

import com.api.weavernorth.action.performance.HandlerPerformance;
import com.engine.kq.biz.KQBalanceOfLeaveBiz;
import net.sf.json.JSONObject;
import org.apache.commons.logging.Log;
import weaver.conn.RecordSet;
import weaver.general.Util;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;

@Path("/weavernorth/action/sick/Sick")
public class Sick extends HttpServlet {
    private static Log log = org.apache.commons.logging.LogFactory.getLog(Sick.class.getName());

    @GET
    @Path("/getData")
    @Produces(MediaType.TEXT_PLAIN)
    public String getDatas(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        RecordSet rs = new RecordSet();
        //判断的变量
        String strflag = "";
        //存放截取后的年月
        String strYearMonth = "";
        //请假人这个月已经请过的时长
        double intMonthSc = 0;
        //存放总时长
        double intZsc = 0;

        //获取前端js传入的请假人姓名和请假类型和时长
        String strQjrXm = request.getParameter("xm");
        log.info("请假人姓名：" + strQjrXm);
        String strQjlx = request.getParameter("qjlx");
        log.info("请假类型：" + strQjlx);
        String strSc = request.getParameter("sc");
        log.info("时长：" + strSc);
        String strKsrq = request.getParameter("ksrq");
        log.info("开始日期：" + strKsrq);

        //请假人姓名转为数组
        String[] strQjrXmSpl = strQjrXm.split(",");
        log.info("请假人姓名转为数组后：" + strQjrXmSpl + ",请假人姓名长度:" + strQjrXmSpl.length);
        //请假类型转为数组
        String[] strQjlxSpl = strQjlx.split(",");
        log.info("请假类型转为数组后：" + strQjlxSpl);
        //时长转为数组
        String[] strScSpl = strSc.split(",");
        log.info("请假时长转为数组后：" + strScSpl[0]);
        //开始日期转为数组
        String[] strKsrqSpl = strKsrq.split(",");
        log.info("请假开始日期转为数组后：" + strKsrqSpl);


        log.info("长度" + strQjrXmSpl.length);
        for (int i = 0; i < strQjrXmSpl.length; i++) {
            log.info("进去循环:"+i);
            if (!"".equals(strKsrqSpl[i])) {
                //循环截取开始日期的年月
                String[] strKsrqSplTwo = strKsrqSpl[i].split("-");

                strYearMonth = strKsrqSplTwo[0] + "-" + strKsrqSplTwo[1];
            }
            log.info("开始循环中，开始日期的年月值：" + strYearMonth);

            //查询状态不为保存的流程的对应数据的时长；
            String strSql = " select detail_duration  from  formtable_main_110_dt1 d,workflow_requestbase w ,formtable_main_110 f where f.id=d.mainid  and f.requestid=w.requestid and currentnodetype!=0 and   detail_resourceId ='" + strQjrXmSpl[i] + "' and  detail_newLeaveType =' " + strQjlxSpl[i] + "' and   to_char(to_date(detail_fromdate,'yyyy-mm-dd'),'yyyy-mm')  ='" + strYearMonth + "' ";
            log.info("查询时长的sql:" + strSql);
            rs.executeQuery(strSql);

            while (rs.next()) {
                //获取请假人已经请过的时长
                String strScTwo = Util.null2String(rs.getString("detail_duration"));
                log.info("请假人已经请过的时长：" + strScTwo);
                //时长转为int类型
                double intScTwo = Double.parseDouble(strScTwo);
                intMonthSc = intMonthSc + intScTwo;
            }
            log.info("请假人之前请过的总时长：" + intMonthSc);
            double intSc = Double.parseDouble(strScSpl[i]);
            log.info("本条流程明细表的时长：" + intSc);
            intZsc = intSc + intMonthSc;
            log.info("本条流程明细数据加上之前请过的所有时长之和：" + intZsc);
            if (intZsc > 1) {
                strflag = "true";
                log.info("flag:" + strflag);
                return strflag;
            } else {
                strflag = "false";
            }
        }
        log.info("flag:" + strflag);
        return strflag;


    }

}
